package factory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * @auther 黄伟
 * @description 工厂构建对象
 * @date 2019/3/26
 */
public class AllFactory {
    static Properties properties =  new Properties();
    static {
      try {
          // 使用InPutStream流读取properties文件
          BufferedReader bufferedReader = new BufferedReader(new FileReader("src/xm.properties"));
          File file = new File("src/xm.properties");
          if (!file.exists()) {
              file.createNewFile();
          }
          properties.load(bufferedReader);
      } catch (IOException e) {
          e.printStackTrace();
      }
  }

    /**
     * getInstance静态方法，用于获取对象
     * @param name
     * @return
     */
    public static Object getInstance(String name) {
        Object fpi = null;
        try {
            Class<?> clazz = Class.forName(properties.getProperty(name));
            fpi = clazz.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            System.out.println("名称" + name + "错误输入") ; // 打印输出信息
        }
        return fpi;
    }
}
